﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Com.Toook.Common;
using System.Data.OleDb;

public partial class joke_content : BaseShowPage
{
    protected string JokeTitle;
    protected string JokeContent;
    protected string PreNext = "";//前一条、后一条

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitJoke();
        }
    }
    private void InitJoke()
    {
        string typePageId = Request["id"];
        int id = 0;
        string type = "";
        string startId = "1";//上一条的Id
        string endId = "3";//下一条的Id
        int currentId = 2;
        bool isValidId = false;//参数是否合法、正确
        //typePageId 9位，范围(510000000, 590000000)
        if (!string.IsNullOrEmpty(typePageId) && typePageId.Length == 9 && typePageId.StartsWith("5"))
        {            
            id = ParamHelper.ParseIntPositive(typePageId);
            if(id > 510000000 && id < 590000000)
            {
                type = typePageId.Substring(0, 2);
                string current = typePageId.Substring(2, 5);//取出中间5位真正的记录序号
                int num = ParamHelper.ParseIntPositive(current);
                if (num > 0)
                {
                    startId = type + (num - 1).ToString("D5") + "00";
                    endId = type + (num + 1).ToString("D5") + "99";
                    isValidId = true;//参数正确
                    currentId = id;
                }
            }
        }

        //

        string sql;
        if (isValidId)
        {
            sql = "select id, typePageId, title, content from PublishJoke where typePageId>=" + startId +
                " and typePageId<=" + endId + " order by id asc ";//升序,将下一条放在第一个。因为目录是倒序的
        }        
        else//不正确
        {
            DataRow row = PageData.SelectParamRow("typeCode='joke'");
            int total = 0;
            int.TryParse(row[PageColumn.typeTotalCount].ToString().Substring(2), out total);
            startId = (total - 1).ToString();//只要下一条
            endId = total.ToString();
            currentId = total;//取最后一条
            sql = "select id, typePageId, title, content from PublishJoke where Id>=" + startId +
                " and Id<=" + endId + " order by id asc";//升序
        } 
        OleDbConnection conn = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        OleDbDataReader reader = null;

        try
        {
            bool first = true;

            conn.Open();
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (isValidId)//根据正确的typePageId查出的结果
                {
                    if (reader["typePageId"].ToString() != currentId.ToString())
                    {
                        if (first)
                        {
                            PreNext = "下一条：<a href=\"content.aspx?id=" + reader["typePageId"].ToString() + "\">" + reader["title"].ToString() + "</a>";
                            first = false;
                        }
                        else
                        {
                            PreNext += "上一条：<a href=\"content.aspx?id=" + reader["typePageId"].ToString() + "\">" + reader["title"].ToString() + "</a>";
                        }
                    }
                    else
                    {
                        JokeContent = reader["content"].ToString();
                        JokeTitle = reader["title"].ToString();
                    }
                }
                else//根据Id查出的结果
                {
                    if (reader["Id"].ToString() != currentId.ToString())
                    {
                        if (first)
                        {
                            PreNext = "下一条：<a href=\"content.aspx?id=" + reader["typePageId"].ToString() + "\">" + reader["title"].ToString() + "</a>";
                            first = false;
                        }
                        else
                        {
                            PreNext += "上一条：<a href=\"content.aspx?id=" + reader["typePageId"].ToString() + "\">" + reader["title"].ToString() + "</a>";
                        }
                    }
                    else
                    {
                        JokeContent = reader["content"].ToString();
                        JokeTitle = reader["title"].ToString();
                    }                    
                }                
            }
        }
        finally
        {
            AccessHelper.CloseDbResource(conn, reader);
        }
    }
}
